package Calc;


import static java.lang.System.out; 

import java.awt.Insets;

import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

import eecs285.proj4.clientWin.ClientMenuFrame;


import java.awt.*;
import java.io.File;

public class CalcFrame extends JFrame
{
    
    public CalcFrame()
    {
        super("Calculator");
        PanelClass ThePanel = new PanelClass();
        add(ThePanel);
        setResizable(false);
        pack();
        Toolkit k = Toolkit.getDefaultToolkit();
        Dimension d = k.getScreenSize();
        setLocation(d.width/3,d.height/4);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void close()
    {
        dispose();
    }

    class PanelClass extends JPanel 
    {
        private JTextField Display;
        private  Insets insets = new Insets(0, 0, 0, 0);
        private JButton One;
        private JButton Two;
        private JButton Three;
        private JButton Four;
        private JButton Five;
        private JButton Six;
        private JButton Seven;
        private JButton Eight;
        private JButton Nine;
        private JButton Zero;
        private JButton Plus;
        private JButton Minus;
        private JButton Multiple;
        private JButton Divide;
        private JButton Equals;
        private JButton Dot;
        private JButton Recip;
        private JButton Sqrt;
        private JButton Percent;
        private JButton MC;
        private JButton MR;
        private JButton MS;
        private JButton MP;
        private JButton MD;
        private JButton Delete;
        private JButton CE;
        private JButton C;
        private JButton Answer;
        Font NumberFont = new Font( "Cambria", 0, 20 );
        Font LetterFont = new Font( "Arial", 0, 14 );
        private ActionListener Mylistener = new Buttonlistener();
        
        private ClientMenuFrame mainFrame;
        
        String TempStr;
        int TempInt;
        double TempValue;
        double CurrentValue;
        double Operand1;
        double Operand2;
        double Result;
        double Memory = 0;
        boolean Normal = true;
        boolean Clear = false;
        private int Operator = 0;
        private int PrevOperator = 0;
        Toolkit k = Toolkit.getDefaultToolkit();
        Dimension d = k.getScreenSize();
        
        int twlv = d.width*12/1920;
        int fv = d.width*5/1920;
        int sixteen = d.width*16/1920;
        public PanelClass() 
        {
           setLayout(new BorderLayout());
           JPanel MyPanel = new JPanel(new GridBagLayout());
           
           JPanel DisplayPanel = new JPanel(new BorderLayout());
           Display = new JTextField("0");
           Display.setFont(NumberFont);
           Display.setBackground(Color.WHITE);
           Display.setHorizontalAlignment(JTextField.RIGHT);
           Display.setEditable(false);
           DisplayPanel.setBorder(BorderFactory.createEmptyBorder(twlv,twlv,0,twlv));
           DisplayPanel.add(Display,BorderLayout.NORTH);
           add(DisplayPanel, BorderLayout.NORTH);
         // addButton(this, Display, 0, 0, 5, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           
          
          Font   poWordStyle  = new Font( "Dialog", 0, 16 );
           MC= new JButton("MC");
           MC.setMargin(new Insets(0,0,0,0)); 
           MC.setFont(LetterFont);
           MC.addActionListener(Mylistener);
           addButton(MyPanel, MC, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           MR = new JButton("MR");
           MR.setMargin(new Insets(0,0,0,0));
           MR.setFont(LetterFont);
           MR.addActionListener(Mylistener);
           
           addButton(MyPanel, MR, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           MS = new JButton("MS");
           MS.setMargin(new Insets(0,0,0,0)); 
           MS.setFont(LetterFont);
           MS.addActionListener(Mylistener);
           addButton(MyPanel, MS, 2, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           // Row Two - Two Buttons
           MP = new JButton("M+");
           MP.setMargin(new Insets(0,0,0,0)); 
           MP.setFont(LetterFont);
           MP.addActionListener(Mylistener);
           addButton(MyPanel, MP, 3, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           MD = new JButton("M-");
           MD.setMargin(new Insets(0,0,0,0)); 
           MD.setFont(LetterFont);
           MD.addActionListener(Mylistener);
           addButton(MyPanel, MD, 4, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           // Row Three - Two Buttons
           
           
           Delete = new JButton("DEL");
           Delete.setMargin(new Insets(0,0,0,0)); 
           Delete.setFont(LetterFont);
           Delete.addActionListener(Mylistener);
           addButton(MyPanel, Delete, 0, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           CE = new JButton("CE");
           CE.setMargin(new Insets(0,0,0,0));
           CE.setFont(LetterFont);
           CE.addActionListener(Mylistener);
           addButton(MyPanel, CE, 1, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           C = new JButton("C");
           C.setMargin(new Insets(0,0,0,0));
           C.setFont(LetterFont);
           C.addActionListener(Mylistener);
           addButton(MyPanel, C, 2, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Answer = new JButton("ANS");
           Answer.setMargin(new Insets(0,0,0,0));
           Answer.setFont(LetterFont);
           Answer.addActionListener(Mylistener);
           addButton(MyPanel, Answer, 3, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Sqrt = new JButton("sqrt");
           Sqrt.setMargin(new Insets(0,0,0,0));
           Sqrt.setFont(LetterFont);
           Sqrt.addActionListener(Mylistener);
           addButton(MyPanel, Sqrt, 4, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           
           
           Seven = new JButton("7");
           Seven.setMargin(new Insets(0,0,0,0));
           Seven.setFont(NumberFont);
           Seven.setBackground(Color.WHITE);
           Seven.addActionListener(Mylistener);
           addButton(MyPanel, Seven, 0, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Eight = new JButton("8");
           Eight.setMargin(new Insets(0,0,0,0));
           Eight.setFont(NumberFont);
           Eight.setBackground(Color.WHITE);
           Eight.addActionListener(Mylistener);
           addButton(MyPanel, Eight, 1, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Nine = new JButton("9");
           Nine.setMargin(new Insets(0,0,0,0));
           Nine.setFont(NumberFont);
           Nine.setBackground(Color.WHITE);
           Nine.addActionListener(Mylistener);
           addButton(MyPanel, Nine, 2, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Divide = new JButton("/");
           Divide.setMargin(new Insets(0,0,0,0));
           Divide.setFont(LetterFont);
           Divide.addActionListener(Mylistener);
           addButton(MyPanel, Divide, 3, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Percent = new JButton("%");
           Percent.setMargin(new Insets(0,0,0,0));
           Percent.setFont(LetterFont);
           Percent.addActionListener(Mylistener);
           addButton(MyPanel, Percent, 4, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           
           
           Four = new JButton("4");
           Four.setMargin(new Insets(0,0,0,0));
           Four.setFont(NumberFont);
           Four.setBackground(Color.WHITE);
           Four.addActionListener(Mylistener);
           addButton(MyPanel, Four, 0, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Five = new JButton("5");
           Five.setMargin(new Insets(0,0,0,0));
           Five.setFont(NumberFont);
           Five.setBackground(Color.WHITE);
           Five.addActionListener(Mylistener);
           addButton(MyPanel, Five, 1, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Six = new JButton("6");
           Six.setMargin(new Insets(0,0,0,0));
           Six.setFont(NumberFont);
           Six.setBackground(Color.WHITE);
           Six.addActionListener(Mylistener);
           addButton(MyPanel, Six, 2, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Multiple = new JButton("*");
           Multiple.setMargin(new Insets(0,0,0,0));
           Multiple.setFont(LetterFont);
           Multiple.addActionListener(Mylistener);
           addButton(MyPanel, Multiple, 3, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Recip = new JButton("1/x");
           Recip.setMargin(new Insets(0,0,0,0));
           Recip.setFont(LetterFont);
           Recip.addActionListener(Mylistener);
           addButton(MyPanel, Recip, 4, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           
           One = new JButton("1");
           One.setMargin(new Insets(0,0,0,0));
           One.setFont(NumberFont);
           One.setBackground(Color.WHITE);
           One.addActionListener(Mylistener);
           addButton(MyPanel, One, 0, 6, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Two = new JButton("2");
           Two.setMargin(new Insets(0,0,0,0));
           Two.setFont(NumberFont);
           Two.setBackground(Color.WHITE);
           Two.addActionListener(Mylistener);
           addButton(MyPanel, Two, 1, 6, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Three = new JButton("3");
           Three.setMargin(new Insets(0,0,0,0));
           Three.setFont(NumberFont);
           Three.setBackground(Color.WHITE);
           Three.addActionListener(Mylistener);
           addButton(MyPanel, Three, 2, 6, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Minus = new JButton("-");
           Minus.setMargin(new Insets(0,0,0,0));
           Minus.setFont(LetterFont);
           Minus.addActionListener(Mylistener);
           addButton(MyPanel, Minus, 3, 6, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Equals = new JButton("=");
           Equals.setMargin(new Insets(0,0,0,0));
           Equals.setFont(poWordStyle);
           Equals.setFont(NumberFont);
           Equals.addActionListener(Mylistener);
           addButton(MyPanel, Equals, 4, 6, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           
           
           Zero = new JButton("0");
           Zero.setMargin(new Insets(0,0,0,0));
           Zero.setFont(NumberFont);
           Zero.setBackground(Color.WHITE);
           Zero.addActionListener(Mylistener);
           addButton(MyPanel, Zero, 0, 7, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Dot = new JButton(".");
           Dot.setMargin(new Insets(0,0,0,0));
           Dot.setFont(NumberFont);
           Dot.setBackground(Color.WHITE);
           Dot.addActionListener(Mylistener);
           addButton(MyPanel, Dot, 2, 7, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           Plus = new JButton("+");
           Plus.setMargin(new Insets(0,0,0,0));
           Plus.setFont(LetterFont);
           Plus.addActionListener(Mylistener);
           addButton(MyPanel, Plus, 3, 7, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
           
           //MyPanel.setSize(new Dimension(320,400));
           MyPanel.setBorder(BorderFactory.createEmptyBorder(fv,twlv,sixteen,twlv));
           add(MyPanel, BorderLayout.SOUTH);
          //setSize(new Dimension(320,320));
           setKeyBindings();
        }
    
        private void setKeyBindings() 
        {
           Action zeroAction = new BindingAction(BindingAction.zero);
           Action oneAction = new BindingAction(BindingAction.one);
           Action twoAction = new BindingAction(BindingAction.two);
           Action threeAction = new BindingAction(BindingAction.three);
           Action fourAction = new BindingAction(BindingAction.four);
           Action fiveAction = new BindingAction(BindingAction.five);
           Action sixAction = new BindingAction(BindingAction.six);
           Action sevenAction = new BindingAction(BindingAction.seven);
           Action eightAction = new BindingAction(BindingAction.eight);
           Action nineAction = new BindingAction(BindingAction.nine);
           Action plusAction = new BindingAction(BindingAction.plus);
           Action minusAction = new BindingAction(BindingAction.minus);
           Action multipleAction = new BindingAction(BindingAction.multiple);
           Action divideAction = new BindingAction(BindingAction.divide);
           Action equalsAction = new BindingAction(BindingAction.equals);
           Action dotAction = new BindingAction(BindingAction.dot);
           int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
           InputMap inputMap = getInputMap(condition);
           ActionMap actionMap = getActionMap();
        
           actionMap.put(BindingAction.zero, zeroAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_0, 0), BindingAction.zero);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD0, 0), 
                   BindingAction.zero);
           
           actionMap.put(BindingAction.one, oneAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), BindingAction.one);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD1, 0),
                 BindingAction.one);
           actionMap.put(BindingAction.two, twoAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_2, 0), BindingAction.two);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, 0),
                 BindingAction.two);
           actionMap.put(BindingAction.three, threeAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_3, 0), BindingAction.three);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD3, 0),
                 BindingAction.three);
           actionMap.put(BindingAction.four, fourAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_4, 0), BindingAction.four);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD4, 0),
                    BindingAction.four);
           actionMap.put(BindingAction.five, fiveAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_5, 0), BindingAction.five);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD5, 0),
                    BindingAction.five);
           actionMap.put(BindingAction.six, sixAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_6, 0), BindingAction.six);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD6, 0),
                    BindingAction.six);
           actionMap.put(BindingAction.seven, sevenAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_7, 0), BindingAction.seven);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD7, 0),
                    BindingAction.seven);
           actionMap.put(BindingAction.eight, eightAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_8, 0), BindingAction.eight);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD8, 0),
                    BindingAction.eight);
           actionMap.put(BindingAction.nine, fourAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_9, 0), BindingAction.nine);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD9, 0),
                    BindingAction.nine);
           actionMap.put(BindingAction.plus, plusAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, KeyEvent.SHIFT_DOWN_MASK), 
                   BindingAction.plus);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0),
                    BindingAction.plus);
           actionMap.put(BindingAction.minus, minusAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, 0), 
                   BindingAction.minus);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0),
                    BindingAction.minus);
           actionMap.put(BindingAction.multiple, multipleAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_8, KeyEvent.SHIFT_DOWN_MASK), 
                   BindingAction.multiple);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_MULTIPLY, 0),
                    BindingAction.multiple);
           actionMap.put(BindingAction.divide, divideAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SLASH, 0), BindingAction.divide);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DIVIDE, 0),
                    BindingAction.divide);
           actionMap.put(BindingAction.dot, dotAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0), BindingAction.dot);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DECIMAL, 0),
                    BindingAction.dot);
           actionMap.put(BindingAction.equals, equalsAction);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, KeyEvent.SHIFT_DOWN_MASK), 
                   BindingAction.equals);
           inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, 0),
                    BindingAction.equals);
    
        }
    
    
    
        private class BindingAction extends AbstractAction 
        {
            public static final String zero = "zero";
            public static final String one = "one";
            public static final String two = "two";
            public static final String three = "three";
            public static final String four = "four";
            public static final String five = "five";
            public static final String six = "six";
            public static final String seven = "seven";
            public static final String eight = "eight";
            public static final String nine = "nine";
            public static final String plus = "plus";
            public static final String minus = "minus";
            public static final String multiple = "multiple";
            public static final String divide = "divide";
            public static final String equals = "equals";
            public static final String dot = "dot";
    
            public BindingAction(String text) 
            {
                super(text);
                putValue(ACTION_COMMAND_KEY, text);
            }
    
            @Override
            public void actionPerformed(ActionEvent evt) 
            {
                String actionCommand = evt.getActionCommand();
                if (actionCommand.equals(zero)) TakeResponse(0);
                else if (actionCommand.equals(one)) TakeResponse(1);
                else if (actionCommand.equals(two)) TakeResponse(2);
                else if (actionCommand.equals(three)) TakeResponse(3);
                else if (actionCommand.equals(four)) TakeResponse(4);
                else if (actionCommand.equals(five)) TakeResponse(5);
                else if (actionCommand.equals(six)) TakeResponse(6);
                else if (actionCommand.equals(seven)) TakeResponse(7);
                else if (actionCommand.equals(eight)) TakeResponse(8);
                else if (actionCommand.equals(nine)) TakeResponse(9);
                else if (actionCommand.equals(plus)) Operate(1);
                else if (actionCommand.equals(minus)) 
                    {out.println("aaaaaaaa");
                    Operate(2);
                    }
                else if (actionCommand.equals(multiple)) Operate(3);
                else if (actionCommand.equals(divide)) Operate(4);
                else if (actionCommand.equals(equals)) 
                {
                    Operand2 = Double.parseDouble(Display.getText().trim());
                    out.println(Operand2);
                    Calc(Operator);
                    Operator = 0;
                    PrevOperator = 0;
                    Clear = true;
                }
                else if (actionCommand.equals(dot)) 
                {
                    if(DotPosition(Display.getText().trim())==-1&&Clear==false)
                    {
                        Display.setText(Display.getText().trim()+".");
                    }
                    else if(Clear==true) Display.setText("0.");
                    Clear = false;
                }
           }
        }
        private void addButton(Container container, JButton Button, int gridx, int gridy,
                int gridwidth, int gridheight, int anchor, int fill) 
        {
              GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
                  anchor, fill, insets, 0, 0);
              gbc.insets = new Insets(3,3,3,3);
              //gbc.weightx = 0.3;
              //gbc.weighty = 0.3;
              Button.setMargin(new Insets(0,0,0,0));
              Button.setPreferredSize(new Dimension(d.height*43/1080,d.height*33/1080));
              container.add(Button, gbc);
        }
        public class Buttonlistener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                if (e.getSource().equals(One)) TakeResponse(1);
                else if (e.getSource().equals(Two)) TakeResponse(2);
                else if (e.getSource().equals(Three)) TakeResponse(3);
                else if (e.getSource().equals(Four)) TakeResponse(4);
                else if (e.getSource().equals(Five)) TakeResponse(5);
                else if (e.getSource().equals(Six)) TakeResponse(6);
                else if (e.getSource().equals(Seven)) TakeResponse(7);
                else if (e.getSource().equals(Eight)) TakeResponse(8);
                else if (e.getSource().equals(Nine)) TakeResponse(9);
                else if (e.getSource().equals(Zero)) TakeResponse(0);
                else if (e.getSource().equals(Plus))
                {
       out.println("here");
                    Operate(1);
                }
                else if (e.getSource().equals(Minus))
                {
             out.println("aaaaaaaa");
                    Operate(2);
                }
                else if (e.getSource().equals(Multiple))
                {
                    Operate(3);
                }
                else if (e.getSource().equals(Divide))
                {
                    Operate(4);
                }
                else if (e.getSource().equals(Dot))
                {
                    if(DotPosition(Display.getText().trim())==-1&&Clear==false)
                    {
                        Display.setText(Display.getText().trim()+".");
                    }
                    else if(Clear==true) Display.setText("0.");
                    Clear = false;
                }
                else if (e.getSource().equals(Sqrt))
                {
                    Operate(5);
                }
                else if (e.getSource().equals(Delete))
                {
                    if(!Display.getText().trim().equals("0"))
                    {
                        TempInt = Display.getText().trim().length();
                        if(TempInt==1)
                        {
                            Display.setText("0");
                        }
                        else 
                        {
                            Display.setText(Display.getText().trim().substring(0,
                                    TempInt-1));
                        }
                    }
                }
                else if (e.getSource().equals(CE))
                {
                    Display.setText("0");
                }
                else if (e.getSource().equals(C))
                {
                    Operator = 0;
                    PrevOperator = 0;
                    Operand1 = Operand2 = 0;
                    Result = 0;
                    Display.setText("0");
                }
                else if (e.getSource().equals(Answer))
                {
                    
                }
                else if (e.getSource().equals(Equals))
                {
                    Operand2 = Double.parseDouble(Display.getText().trim());
           out.println(Operand2);
                    Calc(Operator);
                    Operator = 0;
                    PrevOperator = 0;
                    Clear = true;
                }
                else if (e.getSource().equals(Recip))
                {
                    Operate(6);
                }
                else if (e.getSource().equals(MC))
                {
                    Memory = 0;
                }
                else if (e.getSource().equals(MR))
                {
                    PrintResult(Memory);
                }
                else if (e.getSource().equals(MS))
                {
                    Memory = Double.parseDouble(Display.getText().trim());
                    if(Memory==123)
                    {
                        try {
                            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                                if ("Nimbus".equals(info.getName())) {
                                    UIManager.setLookAndFeel(info.getClassName());
                                    break;
                                }
                            }
                        } catch (Exception e1) {
                            // If Nimbus is not available, you can set the GUI to another look and feel.
                        }
                        mainFrame = new ClientMenuFrame("Underground Broadcast");
                        close();
                    }
                }
                else if (e.getSource().equals(MP))
                {
                    Memory +=Double.parseDouble(Display.getText().trim());
                }
                else if (e.getSource().equals(MD))
                {
                    Memory -=Double.parseDouble(Display.getText().trim());
                }
                else if (e.getSource().equals(Percent))
                {
                    Operate(7);
                }
            }
        }
        public void TakeResponse(int a)
        {
            if(a>0&&a<=9)
            {
                if(Display.getText().trim().equals("0")||Clear==true)
                {
                    Display.setText(a+"");
                    Clear = false;
                }
                else Display.setText(Display.getText().trim()+a);
            }
            else if(a==0)
            {
                if(!Display.getText().trim().equals("0"))
                    Display.setText(Display.getText().trim()+"0");
                else if(Clear==true) 
                {
                    Display.setText("0");
                    Clear = false;
                }
            }
                
        }
        public int DotPosition(String a)
        {
            return a.indexOf('.');
        }
        public void Operate(int a)
        {
            Clear = true;
            PrevOperator = Operator;
            Operator = a;
            if(PrevOperator!=0)
            {
                Normal = false;
                Operand2 = Double.parseDouble(Display.getText().trim());
                Calc(PrevOperator);
            }
            else Operand1 = Double.parseDouble(Display.getText().trim());
    out.println(Operand1);
        }
        public void Calc(int a)
        {
            if(1<=a&&a<=4)
            {
                if(a==1)
                {
                    Result = Operand1 + Operand2;
                }
                else if(a==2)
                {
                    Result = Operand1 - Operand2;
                }
                else if(a==3)
                {
                    Result = Operand1 * Operand2;
                }
                else if(a==4)
                {
                    Result = Operand1 / Operand2;
                }
      out.println(Result);
                Operand1 = Result;
                PrintResult(Result);
            }
            else
            {
                if(Normal==false)
                {
                    if(a==5)
                    {
                        if(Operand2>=0) Operand2 = Math.sqrt(Operand2);
                    }
                    else if(a==6)
                    {
                        if(Operand2!=0) Operand2 = 1/Operand2;
                    }
                    Calc(PrevOperator);
                }
                else
                {
                    if(a==5)
                    {
                        if(Operand1>=0) Result = Math.sqrt(Operand1);
                    }
                    else if(a==6)
                    {
                        if(Operand1!=0) Result = 1/Operand1;
                    }
                    Operand1 = Result;
                    PrintResult(Result);
                }
            }
        }
        public void PrintResult(double a)
        {
            TempStr = a+"";
            TempInt = DotPosition(TempStr);
            if(TempInt==-1) Display.setText(TempStr);
            else if(a==0) Display.setText("0");
            else
            {
                //if(FindSignificance(TempStr,TempInt) - TempInt>)
               // java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
                Display.setText(TempStr.substring(0,1+FindSignificance(TempStr,TempInt)));
            }
        }
        public int FindSignificance(String a, int Dotpos)
        {
            int i;
            int S = a.length()-1;
            for(i=S;i>=Dotpos-1;i--)
            {
                if(a.charAt(i)!='0'&&a.charAt(i)!='.') return i;
            }
            return S;
        }
    }
}